package edu.bulsu.coe.sql;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import telco.com.conn.ConnectionFactory;
import telco.com.dao.TelcoBusFareDAO;
import telco.com.dao.TelcoFareInfoDAO;
import telco.com.dao.TelcoMINLoadDAO;
import telco.com.dao.TelcoNFCInfoDAO;
import telco.com.vo.TelcoFareInfoVO;
import telco.com.vo.TelcoMINLoadVO;
import telco.com.vo.TelcoNFCInfoVO;

public class SQL {
	TelcoMINLoadDAO telcoMINLoadDAO;
	TelcoMINLoadVO telcoMINLoadVO;
	TelcoFareInfoDAO telcoFareInfoDAO;
	TelcoFareInfoVO telcoFareInfoVO;
	
	HashMap<String, ArrayList<String>> fareResultSet;
	public SQL(String sqlAddr, String sqlPort, String sqlSID, String sqlUser, String sqlPass){
		ConnectionFactory.setUserName(sqlUser);
		ConnectionFactory.setPassword(sqlPass);
		ConnectionFactory.setConnectionUrl(sqlAddr, sqlPort, sqlSID, null);
		ConnectionFactory.setDriverClassName("oracle.jdbc.OracleDriver");
		TelcoBusFareDAO telcoBusFareDao = new TelcoBusFareDAO();
		try{
			telcoBusFareDao.findAll();
			fareResultSet = telcoBusFareDao.getFindAllResult();
		}catch(SQLException sqle){
			fareResultSet = null;
		}
	}
	
	//[ERR] Code 211 RegularFlatrate is set to default
	//@Deprecated
	public float getRegularFlatrate(){
		float regularFlatrate = 50f;
		try{
			regularFlatrate = Float.parseFloat(fareResultSet.get("RE").get(1));
		}catch(Exception e){
			System.out.println("[SQL] Warning 211: Regular Flatrate was set to default [50f]");
		}
		return regularFlatrate;
	}
	//[ERR] Code 212 SeniorFlatrate is set to default
	//@Deprecated
	public float getSeniorFlatrate(){
		float seniorFlatrate = 40f;
		try{
			seniorFlatrate = Float.parseFloat(fareResultSet.get("SE").get(1));
		}catch(Exception e){
			System.out.println("[SQL] Warning 212: Senior Flatrate was set to default [40f]");
		}
		return seniorFlatrate;
	}
	//[ERR] Code 213 StudentFlatrate is set to default
	//@Deprecated
	public float getStudentFlatrate(){
		float studentFlatrate = 40f;
		try{
			studentFlatrate = Float.parseFloat(fareResultSet.get("ST").get(1)); 
		}catch(Exception e){
			System.out.println("[SQL] Warning 213: Student Flatrate was set to default [40f]");
		}
		return studentFlatrate;
	}
	//[ERR] Code 214 DisabledFlatrate is set to default
	//@Deprecated
	public float getDisabledFlatrate(){
		float disabledFlatrate = 35f;
		try{
			disabledFlatrate = Float.parseFloat(fareResultSet.get("DI").get(1));
		}catch(Exception e){
			System.out.println("[SQL] Warning 214: Disabled Flatrate was set to default [35f]");
		}
		return disabledFlatrate;
	}
	
	//[ERR] Code 221 Load is set to testing
	//@Deprecated
	public float getLoad(int cardNumber){
		TelcoMINLoadDAO telcoMINLoadDAO = new TelcoMINLoadDAO();
		TelcoMINLoadVO telcoMINLoadVO = new TelcoMINLoadVO();
		try{
			telcoMINLoadDAO.performGetTelcoMINLOADVO(telcoMINLoadVO, String.valueOf(cardNumber));
			return telcoMINLoadVO.getBalance();
		}catch(SQLException sqle){
			System.out.println("[SQL] Warning 221: Load was set to default [100f]");
			return 100f;
		}
	}
	//[ERR] Code 222 Debt is set to testing
	public float getDebt(int cardNumber){
		TelcoMINLoadDAO telcoMINLoadDAO = new TelcoMINLoadDAO();
		TelcoMINLoadVO telcoMINLoadVO = new TelcoMINLoadVO();
		try{
			telcoMINLoadDAO.performGetTelcoMINLOADVO(telcoMINLoadVO, String.valueOf(cardNumber));
			return telcoMINLoadVO.getDebt();
		}catch(SQLException sqle){
			
			System.out.println("[SQL] Warning 222: Debt was set to default [0f]");
			return 0f;
		}
	}
	
	//[ERR] Code 231 Fare is set to testing
	public float getFare(String cardNumber, float distance, int studentCount, int disabledCount, int seniorCount, int regularCount){
		TelcoFareInfoDAO telcoFareInfoDAO = new TelcoFareInfoDAO();
		TelcoFareInfoVO telcoFareInfoVO = new TelcoFareInfoVO();
		try {
			telcoFareInfoDAO.performGetTelcoFareVO(telcoFareInfoVO,
					cardNumber,
					distance,
					studentCount,
					disabledCount,
					seniorCount,
					regularCount);
			return telcoFareInfoVO.getFare();
		} catch (SQLException e) {
			System.out.println("[SQL] Warning 231: Fare was set to default [0f]");
			return 0f;
		}
	}
	
	//[ERR] Code 231 Status is set to testing
	public String getNFCStatus(int cardNumber) {
		TelcoNFCInfoDAO telcoNFCInfoDAO = new TelcoNFCInfoDAO();
		TelcoNFCInfoVO telcoNFCInfoVO = new TelcoNFCInfoVO();
		try {
			telcoNFCInfoDAO.performGetTelcoNFCInfoVO(telcoNFCInfoVO, String.valueOf(cardNumber));
			String status = telcoNFCInfoVO.getStatus(); 
			if(status == null) status = "DEACTIVATED";
			return status;
		} catch (SQLException e) {
			System.out.println("[SQL] Warning 241: Status was set to deactivated [DEACTIVATED]");
			return "DEACTIVATED";
		}
	}
}
